package cn.iocoder.yudao.module.ride.controller.admin.extend;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ride.controller.admin.extend.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.extend.ExtendDO;
import cn.iocoder.yudao.module.ride.service.extend.ExtendService;

@Tag(name = "管理后台 - 司机扩展")
@RestController
@RequestMapping("/ride/extend")
@Validated
public class ExtendController {

    @Resource
    private ExtendService extendService;

    @PostMapping("/create")
    @Operation(summary = "创建司机扩展")
    @PreAuthorize("@ss.hasPermission('ride:extend:create')")
    public CommonResult<Long> createExtend(@Valid @RequestBody ExtendSaveReqVO createReqVO) {
        return success(extendService.createExtend(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新司机扩展")
    @PreAuthorize("@ss.hasPermission('ride:extend:update')")
    public CommonResult<Boolean> updateExtend(@Valid @RequestBody ExtendSaveReqVO updateReqVO) {
        extendService.updateExtend(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除司机扩展")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ride:extend:delete')")
    public CommonResult<Boolean> deleteExtend(@RequestParam("id") Long id) {
        extendService.deleteExtend(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除司机扩展")
                @PreAuthorize("@ss.hasPermission('ride:extend:delete')")
    public CommonResult<Boolean> deleteExtendList(@RequestParam("ids") List<Long> ids) {
        extendService.deleteExtendListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得司机扩展")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ride:extend:query')")
    public CommonResult<ExtendRespVO> getExtend(@RequestParam("id") Long id) {
        ExtendDO extend = extendService.getExtend(id);
        return success(BeanUtils.toBean(extend, ExtendRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得司机扩展分页")
    @PreAuthorize("@ss.hasPermission('ride:extend:query')")
    public CommonResult<PageResult<ExtendRespVO>> getExtendPage(@Valid ExtendPageReqVO pageReqVO) {
        PageResult<ExtendDO> pageResult = extendService.getExtendPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExtendRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出司机扩展 Excel")
    @PreAuthorize("@ss.hasPermission('ride:extend:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExtendExcel(@Valid ExtendPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExtendDO> list = extendService.getExtendPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "司机扩展.xls", "数据", ExtendRespVO.class,
                        BeanUtils.toBean(list, ExtendRespVO.class));
    }

}